﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;

using DragDraw;
using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw.ClientTest
{
    [TestClass]
    public class TestWorldMgr:SilverlightTest
    {
        private UserInfo userInfo = new UserInfo() { UserID = "aaa", Realname = "bbb" };
        private string password = "123";
        [TestInitialize]
        [Asynchronous]
        public void Init()
        {
            bool reg = false;
            WorldMgr.Instance.OnRegister +=
                (object sender, RegisterCompletedEventArgs e) =>
                {
                    reg = true;
                };
            WorldMgr.Instance.OnError +=
                (object sender, OnErrorEventArgs e) =>
                {
                    reg = true;
                };
            //test register
            EnqueueCallback(() => WorldMgr.Instance.Register(userInfo, password));
            EnqueueConditional(() => reg);
            EnqueueTestComplete();
        }
        [TestCleanup]
        public void Dispose()
        {
        }
        [TestMethod]
        [Asynchronous]
        public void TestLogin()
        {
            
            LoginCompletedEventArgs loginRet = null;
            
            WorldMgr.Instance.OnLogin +=
                (object sender, LoginCompletedEventArgs e) =>
                {
                    loginRet = e;
                };

            EnqueueCallback(() =>
            {
                WorldMgr.Instance.Login(userInfo.UserID, password);
            }
            );
            //test login
            EnqueueConditional(() => loginRet != null);
            EnqueueCallback(() =>
                {
                    Assert.AreEqual(null, loginRet.Error);
                    Assert.AreEqual(true, loginRet.Result);
                }
            );
            EnqueueTestComplete();
        }
        [TestMethod]
        [Asynchronous]
        public void TestGetUserInfo()
        {
            GetUserInfoCompletedEventArgs ret = null;
            WorldMgr.Instance.OnGetUserInfo +=
                (object sender, GetUserInfoCompletedEventArgs e) =>
                {
                    ret = e;
                };
            EnqueueCallback(() => WorldMgr.Instance.LoadUserInfo(userInfo.UserID));
            EnqueueConditional(() => ret != null);
            EnqueueCallback(() =>
                {
                    Assert.AreEqual(null, ret.Error);
                    Assert.AreEqual(userInfo.Realname, ret.Result.Realname);
                }
            );
            EnqueueTestComplete();
        }
        [TestMethod]
        [Asynchronous]
        public void TestSetUserInfo()
        {
            SetUserInfoCompletedEventArgs setret = null;
            UserInfo newUser = new UserInfo();
            newUser.UserID = userInfo.UserID;
            newUser.Realname = "xxxx";
            WorldMgr.Instance.OnSetUserInfo +=
                (object sender, SetUserInfoCompletedEventArgs e) =>
                {
                    setret = e;
                };
            GetUserInfoCompletedEventArgs getret = null;
            WorldMgr.Instance.OnGetUserInfo +=
                (object sender, GetUserInfoCompletedEventArgs e) =>
                {
                    getret = e;
                };
            //set new user info
            EnqueueCallback(() => WorldMgr.Instance.SaveUserInfo(newUser));
            EnqueueConditional(() => setret != null);
            EnqueueCallback(() =>
            {
                Assert.AreEqual(null, setret.Error);
                Assert.AreEqual(true, setret.Result);
            }
            );
            //load and see if it is modified correctly
            EnqueueCallback(() => WorldMgr.Instance.LoadUserInfo(userInfo.UserID));
            EnqueueConditional(() => getret != null);
            EnqueueCallback(() =>
            {
                Assert.AreEqual(null, getret.Error);
                Assert.AreEqual(newUser.Realname, getret.Result.Realname);
            }
            );
            //set to origin
            EnqueueCallback(() => WorldMgr.Instance.SaveUserInfo(userInfo));
            EnqueueConditional(() => setret != null);
            EnqueueCallback(() =>
            {
                Assert.AreEqual(null, setret.Error);
                Assert.AreEqual(true, setret.Result);
            }
            );
            EnqueueTestComplete();
        }
    }
}
